/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	深圳市国省检日常化监管系统         </p>
 * <p>创建者   :	win7 
 * 
 * <p>描   述  :   DigitalServiceImpl.java for com.pengtu.gsj.service.impl.check.jsbz.informatization    </p>
 * 
 * <p>最后修改 : $: 2017年8月4日-下午2:23:42 v 1.0.0	 win7   $ </p>
 * 
*/

package com.pengtu.gsj.service.impl.check.jsbz.informatization;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.base.QueryFilter.MatchType;
import com.pengtu.gsj.dao.check.jsbz.informatization.DigitalDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.jsbz.InfoManagement;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.jsbz.informatization.DigitalService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;

/**
 * 
 * DigitalServiceImpl
 * 
 * 2017年8月4日 下午2:23:42
 * 
 * @version 1.0.0
 * 
 */
@Service
@Transactional
public class DigitalServiceImpl implements DigitalService {
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	public DigitalDao digitalDao;
	
	@Autowired
	private CheckTableService checkTableService;
	
	public List<InfoManagement> getInfoManagementList(QueryFilter queryFilter){
		return digitalDao.find(queryFilter);
	}
	
	@Override
	public Map<String, Object> getDigital(String stratTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<Object> values = new ArrayList<Object>();
		List<InfoManagement> infoManagementList = new ArrayList<InfoManagement>();
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		String systemNameId = (String)jsonObject.get("systemNameId");
		//取到所有文件
		List<Attachment> list = systemManageService.getCheckAttachmentsByPartId(stratTime, endTime, tag, systemNameId);
		//查询数据
		QueryFilter queryFilter = new QueryFilter();
		//查询得分情况
		double total = yearPart;		//检查得分
		double percentage = 0.0;		//完善度
		double sum = yearFull + yearPart;	//表格总分
		
		//1表示实现数据整合
		if (StringUtils.isEmpty(systemNameId)) {
			infoManagementList = this.getInfoManagementList(queryFilter);
		} else {
			queryFilter.addFilter("id", systemNameId, MatchType.EQ);
			infoManagementList = this.getInfoManagementList(queryFilter);
			for (InfoManagement InfoManagement : infoManagementList) {
				if (InfoManagement.getIsShare().equals("1")) {
					total += yearFull;
				} 
				InfoManagement.setRemark(String.valueOf(total));
			}
			values.add(systemNameId);
			//判断完善度
			percentage = MathsUtils.divDouble(total, sum, 2);
			percentage  = MathsUtils.formatDouble(percentage*100, 2);
			map.put("percent", percentage);
			map.put("infoManagement", infoManagementList);
			map.put("att", list);
		}
						
		
		map.put("checkTable", checkTable);
		map.put("sum", total);
		checkTableService.updateCheckTable(checkTable, percentage, total, sum, values);
		map.put("score", checkTable.getTestFull());
		return map;
	}
}
